var React = require('react');
import $ from 'jquery';
import {browserHistory,Link} from 'react-router';

var Login = React.createClass({
	getInitialState: function() {
		return {
		  isLogin: true
		};
	},
	submitForm:function(e){
		e.preventDefault();
		let username=this.refs.username.value;
		let pwd=this.refs.password.value;
		var self=this;
		var data={
			LoginName:username,
			Password:pwd
		};
		let url=this.state.isLogin?"http://www.see2eyes.cn/Todo/Login":"http://www.see2eyes.cn/Todo/Regist";
		$.ajax({
            type: "POST",
            url: url,
            dataType: "json",
            data: data,
            success: function (result) {                
                if(result.state=="success")
            	{            		
            		const path = `#/todo/${result.uid}`
            		// browserHistory.push(path);
            		window.location.hash = path; // 被点击元素的‘key’的值‘about’就是页面跳转的路径
        		}
                else{
                	alert(result.msg);
					self.refs.password.value="";
                }
            }
        });
	},
	changeType:function(){
		this.setState({
			isLogin:!this.state.isLogin
		})
	},
  	render: function () {
	    return (	      
	      <form onSubmit={this.submitForm}>
	      	<h2>{this.state.isLogin?"登录":"注册"}</h2>
		      <table>
			      <tbody>
			      	<tr>
			      		<td>用户名：</td>
			      		<td>
			      			<input type="text" ref="username" />
			      		</td>
			      	</tr>
			      	<tr>
			      		<td>密码</td>
			      		<td>
			      			<input type="password" ref="password" />
			      		</td>
			      	</tr>
			      	<tr>
			      		<td></td>
			      		<td>
			      			<input type="submit" value={this.state.isLogin?"登录":"注册"} />
			      			<a href="#" style={{marginLeft:"25px",fontSize:'12px'}} onClick={this.changeType}>{this.state.isLogin?"注册":"登录"}</a>

			      			<Link to="/todo/2" activeClassName="active">TODO2</Link>
			      		</td>
			      	</tr>
			      </tbody>		      	
		      </table>
	      </form>
	    );
	  }
});

module.exports = Login;